package com.cc.controller;

import com.cc.model.EbNews;
import com.cc.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    private Model model;
    private Integer enId;

    @RequestMapping("to_save")
    public String toSave(){
        return "manage/news-add";
    }

    @RequestMapping("/save_commit")
    public String saveCommit(EbNews ebNews){
        System.out.println("ebNews"+ebNews);
        //补充添加时间
        //获取系统时间
        Date date = new Date();
        //Date---->String
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date);
        //System.out.println("timeStr=="+timeStr);
        ebNews.setEnCreateTime(timeStr);
        //1.获取表单的值---参数，参数校验
        if((ebNews.getEnTitle()==null||
                ebNews.getEnTitle().equals("")||
                ebNews.getEnTitle().trim().length()==0)
                ||(ebNews.getEnContent()==null||
                ebNews.getEnContent().equals("")||
                ebNews.getEnContent().trim().length()==0)){
            return "manage/manage-error";//error.jsp
        }

        //2.调service保存的方法
        int result = newsService.save(ebNews);
        if(result<=0){
            //return或异常
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        //1.参数校验

        //2.执行删除
        int result = newsService.deleteByPk(id);
        if(result <= 0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/edit")
    public String edit(Model model, Integer enId){
        //1.根据新闻的编号查询新闻的详细信息
        EbNews ebNews = newsService.selectByCondition(enId);

        //2.将查出来的新闻信息传递给news-modify.jsp
        model.addAttribute("ebNews",ebNews);
        return "manage/news-modify";
    }
    
    @RequestMapping("/editSubmit")
    public String editSubmit(EbNews ebNews){

        //1.更新新闻信息
        int result = newsService.updateById(ebNews);
        //2.跳转到更新结果页面
        if(result <= 0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
        //1.获取新闻表的第一页数据，每页三条
        List<EbNews> newses = newsService.list(page,pageSize);
        int maxPage = newsService.getMaxPage(pageSize);
        //2.将数据（list） 和视图（jsp）返回String
        ModelAndView mv = new ModelAndView();
        //绑定数据
        mv.addObject("newses",newses);
        //绑定视图

        mv.addObject("curPage",page);

        mv.addObject("maxPage",maxPage);

        mv.setViewName("manage/news");

        return mv;
    }
}